const db = wx.cloud.database()
const Time = require('../../../utils/time/TimeUtil2')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    now_time: '',
    status: 0, //0---》上班未打卡 1----》上班已打卡 
    now_time_stop: '', //已打卡时间
    user: {}, //用户
    xiangni: {},
  },


  onLoad: function (options) {
    let value = wx.getStorageSync('autt')
    this.getCurrentTime();
    this.setData({
      now_time: this.getTime(),
      today_date: this.formatDate(new Date()),
      user: value.user,
    })
    this.getxiangta()
  },

  onShow: function () {

  },
  //获取想你次数
  getxiangta() {
    let id = this.data.user._id
    console.log(id);
    let sj
    db.collection('xiangta').where({
      cpid: id
    }).get({
      success: res => {
        sj = res.data
        this.setData({
          xiangni: sj
        })
      }
    })
  },
  // 签到打卡
  async clockInStart() {
    try {
      var value = wx.getStorageSync('sj')
      if (value) {
        let diyici = new Date()
        if (diyici.getTime() - value > 3600000) {
          this.daka()
        } else {
          wx.showToast({
            title: '一个小时后才可以继续哟~~',
            icon: 'none',
            duration: 2000
          })
        }
      } else {
        this.daka()
      }
    } catch (e) {
      // Do something when catch error
    }

  },
  //daka
  async daka() {
    wx.vibrateLong(); //使手机震动400ms
    //获取当前时间戳
    let diyici = new Date()
    console.log(diyici.getTime());
    wx.setStorageSync('sj', diyici.getTime())
    const _ = db.command
    //获取将数据发送到数据库
    let user = this.data.user
    let str = {
      date: '' + this.data.today_date + " " + this.data.now_time,
      text: user.aut + '想你了'
    }
    let arr = []
    await db.collection('xiangta').doc(user.xiangtaid).get({
      success: res => {
        arr = res.data.xiangta
        arr.unshift(str)
        db.collection('xiangta').doc(user.xiangtaid).update({
          data: {
            xiangta: arr
          },
          success: res => {
            wx.showToast({
              title: '耶~~~积分+8',
              icon: 'none'
            })
          }
        })
      }
    })
    this.setData({
      status: 1, //上班已打卡
      now_time_stop: this.data.now_time,
    })
    console.log('开始计时');
    setTimeout(() => {
      this.setData({
        status: 0,
      })
    }, 76000);
    this.openDemJF(8, '想ta')
  },

  getCurrentTime: function () {
    var time = setInterval(() => {
      this.setData({
        now_time: this.getTime()
      })
    }, 1000)
  },
  getTime() {
    let dateTime = '';
    let hh = new Date().getHours()
    let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() :
      new Date().getMinutes()
    let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() :
      new Date().getSeconds()
    dateTime = hh + ':' + mf + ':' + ss;
    return dateTime;
  },

  // 获取当前的日期
  formatDate(timestamp) {
    var date = new Date(timestamp);
    var YY = date.getFullYear() + '-';
    var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
    var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
    return YY + MM + DD;
  },
  //加积分
  async openDemJF(num, string) {
    var value = wx.getStorageSync('autt')
    console.log("jifebn");
    //连接数据库\
    let uup = db.command
    let tishi = '积分+' + num
    let bz = string + '+' + num
    const _ = db.command
    //获取当前时间
    let tim = Time.getDangQianRiQi()
    let datad = Time.formatDatetime(tim)
    let dx = {
      nr: bz,
      time: datad
    }
    //获取本地数据
    let ve = wx.getStorageSync('autt')
    let arres = ve.user.jfmingxi
    if (arres.length > 50) {
      arres = arres.slice(0, 50)
    }
    arres.unshift(dx)
    await db.collection('users').doc(value.user._id).update({
      // data 传入需要局部更新的数据
      data: {
        jf: uup.inc(num),
        jfmingxi: arres
      },
      success: function (res) {
        console.log("++++>");
      }
    })
  }
})